package com.mars.manager.user.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mars.base.common.exception.BizException;
import com.mars.base.common.utils.RandomUitl;
import com.mars.base.common.utils.security.PasswordUtils;
import com.mars.base.common.utils.web.ResponseResult;
import com.mars.base.service.BaseService;
import com.mars.manager.user.entity.User;
import com.mars.manager.user.exception.UserBizException;
import com.mars.manager.user.mapper.UserMapper;
import com.mars.manager.user.service.UserService;

@Service
public class UserServiceImpl extends BaseService<User, Long> implements UserService {
	
	@Autowired
	private UserMapper mapper ;

	@Override
	public int insertSelective(User entity) throws UserBizException {
		entity.setSalt(RandomUitl.uuid());
		//密码加密加盐，使用手机号码作为登陆账号，手机号码后4位为初始密码
		entity.setPassword(PasswordUtils.encodePassword(entity.getMobile().substring(entity.getMobile().length()-4), entity.getSalt()));
		
		return super.insertSelective(entity);
	}
	
	@Override
	public int updateSelective(User entity) throws BizException {
		return super.updateSelective(entity);
	}
	
	@Override
	public User selectById(Long id) throws BizException {
		return super.selectById(id) ;
	}

	@Override
	public User gerUserByUserName(String username) {
		User entity = new User() ;
		entity.setUsername(username); 
		return super.selectOne(entity) ;
	}

	@Override
	public Boolean checkData(String content, Integer type) {
		if(type == 1) {// 用户名是否存在
			User checkValue = this.mapper.checkValue("username", content) ;
			return checkValue != null ? true : false ;
		} else if(type == 2) {// 邮件地址是否存在
			User checkValue = this.mapper.checkValue("email", content) ;
			return checkValue != null ? true : false ;
		} else if(type == 3) {// 手机号码是否存在
			User checkValue = this.mapper.checkValue("mobile", content) ;
			return checkValue != null ? true : false ;
		}
		return null;
	}

	@Override
	public ResponseResult resetPassword(Long[] ids) {
		int successCount = 0, failCount = 0;
		for (Long id : ids) {
			User user = this.selectById(id);
			if(null != user) {
				if(null != user.getMobile() && StringUtils.isNotEmpty(user.getMobile())) {
					String password = user.getMobile().substring(user.getMobile().length()-4) ;
					String slat = RandomUitl.uuid() ;
					String encodePassword = PasswordUtils.encodePassword(password, slat) ;
					
					Integer updatePassword = this.mapper.resetPassword(user.getId(), encodePassword, slat) ;
					if(updatePassword > 0) {
						successCount ++ ;
					} else {
						failCount ++ ;
					}
				} else {
					return ResponseResult.build(true, "该用户["+user.getRealname()+"]未设置手机号码，终止重置密码") ;
				}
			}
		}
		
		Map<String, Object> mapData = new HashMap<String, Object>() ;
		mapData.put("successCount", successCount) ;
		mapData.put("failCount", failCount) ;
		return ResponseResult.build(true, "重置密码", mapData) ;
		
	}

	@Override
	public Integer updateAccountStatus(Long[] ids, Integer status) {
		return this.mapper.updateAccountStatus(ids, status);
	}

	@Override
	public boolean validatePassword(String encPassword, String password, String salt) {
		return PasswordUtils.isPasswordValid(encPassword, password, salt) ;
	}

	@Override
	public User getAccount(String account) {
		return this.mapper.getAccount(account);
	}

}
